const { exec } = require('../../mysql')

function getNowTime(){
    let nowDate = new Date()
    let year = nowDate.getFullYear()
    let month = nowDate.getMonth()+1
    let day = nowDate.getDate()
    return `${year}-${month}-${day}`
}

// 获取所有的临时员工列表  get 无参
const getTemporaryStaffList = () =>{
    let sql = `select * from temporaryStaff;`
    return exec(sql)
}

// 分页获取临时员工列表 post 参数：page，pageSize
const pageGetTemporaryStaffList = (page,pageSize) => {
    let sql = `select * from temporaryStaff limit ${(page-1)*pageSize},${pageSize};`
    return exec(sql)
}

// 模糊查询员工名 post 参数为员工名
const searchTemporaryStaffName = (staffName) => {
    let sql = `select * from temporaryStaff where name like '%${staffName}%';`
    return exec(sql)
}

// 搜索入职时间 post 参数为入职时间
const searchEntryStaff = (time) =>{
    let sql = `select * from temporaryStaff Where entryTime Between DATE_ADD('${time}',INTERVAL -7 DAY) And DATE_ADD('${time}',INTERVAL 7 DAY);`
    return exec(sql)
}

// 分页查询员工入职时间 post time，page，pageSize
const pageSearchEntryStaff = (time,page,pageSize) => {
    let sql = `select * from temporaryStaff Where entryTime Between DATE_ADD('${time}',INTERVAL -7 DAY) And DATE_ADD('${time}',INTERVAL 7 DAY) limit ${(page-1)*pageSize},${pageSize};`
    return exec(sql)
}

// 转正 post 参数为实习员工id 转正后的name,age,gender,phone,address,salary
const becomeOfficialStaff = (id,name,age,gender,phone,address,salary) => {
    let entryTime = getNowTime()
    let delSql = `delete from temporaryStaff where id = ${id};`
    let addSql = `insert into officialStaff (name,age,gender,phone,address,entryTime,salary) values(
        '${name}',${age},'${gender}',${phone},'${address}','${entryTime}',${salary}
        );`
    return exec(delSql).then(delData => {
        if (delData.affectedRows > 0) {
            exec(addSql).then(addData => {
                if(addData.affectedRows > 0) {
                    return true
                }
                return false
            })
            return true
        }
        return false
    })
}

// 批量删除 post 参数为批量删除数据id组成的数组
const batchDeleteTemporaryStaff = (idAry) => {
    let sql = `delete from temporaryStaff where id in (${idAry});`
    return exec(sql).then(delData => {
        if (delData.affectedRows > 0) {
            return true
        }
        return false
    })
}

// 增加 post 参数：name,age,gender,phone,address,salary,trialDay
const addTemporaryStaff = (name,age,gender,phone,address,salary,trialDay) => {
    let entryTime = getNowTime()
    let sql = `insert into temporaryStaff (name,age,gender,phone,address,entryTime,salary,trialDay) values(
        '${name}',${age},'${gender}',${phone},'${address}','${entryTime}',${salary},${trialDay}
        );
    `
    return exec(sql).then(insertData => {
        if (insertData.affectedRows > 0) {
            return true
        }
        return false
    })
}

// 修改 post 参数：id, name,age,gender,phone,address,duty,salary,trialDay
const updateTemporaryStaff = (id,name,age,gender,phone,address,salary,trialDay) => {
    let sql = `update temporaryStaff set name = '${name}',age = ${age},gender = '${gender}',
        phone = ${phone},address = '${address}',salary = ${salary},trialDay=${trialDay} where id = ${id};
    `
    return exec(sql).then(Data => {
        if (Data.affectedRows > 0) {
            return true
        }
        return false
    })
}


module.exports = {
    getTemporaryStaffList,
    pageGetTemporaryStaffList,
    searchTemporaryStaffName,
    addTemporaryStaff,
    batchDeleteTemporaryStaff,
    updateTemporaryStaff,
    becomeOfficialStaff,
    searchEntryStaff,
    pageSearchEntryStaff
}